package org.csu.mypetstore.controller;

import jakarta.servlet.http.HttpSession;
import org.csu.mypetstore.aspect.LogAnnotation;
import org.csu.mypetstore.commonResponse.CommonResponse;
import org.csu.mypetstore.domin.Account;
import org.csu.mypetstore.domin.Cart;
import org.csu.mypetstore.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.csu.mypetstore.controller.AccountController.isLogin;

@RestController
@RequestMapping("/carts")
public class CartController {
    @Autowired
    CartService cartService;
    @LogAnnotation(module="carts",operation = "查看购物车")
    //查找我的所有购物车
    @GetMapping("")
    CommonResponse<List<Cart>> getCartList(HttpSession session){
        CommonResponse<Account> commonResponse=isLogin(session);
        if (commonResponse.getStatus() == 0) {
            return CommonResponse.createForError("未登录");
        }
        return cartService.getCartList(commonResponse.getData().getUserid());
    }

    //根据itemId查找我的购物车
    @LogAnnotation(module="carts",operation = "根据itemid查看购物车")
    @GetMapping("/{itemId}")
    CommonResponse<Cart> getCart(@PathVariable String itemId,HttpSession session){
        CommonResponse<Account> commonResponse=isLogin(session);
        if (commonResponse.getStatus() == 0) {
            return CommonResponse.createForError("未登录");
        }
        return cartService.getCart(itemId,commonResponse.getData().getUserid());
    }


    //增加我的购物车
    @LogAnnotation(module="carts",operation = "增加购物车")
    @PostMapping("")
    CommonResponse<Cart> insterToCart(@RequestBody Cart cart, HttpSession session){
        CommonResponse<Account> commonResponse=isLogin(session);
        if (commonResponse.getStatus() == 0) {
            return CommonResponse.createForError("未登录");
        }
        cart.setUsername(commonResponse.getData().getUserid());
        return cartService.InsterToCart(cart);
    }

    //删除itemId购物车
    @LogAnnotation(module="carts",operation = "删除购物车")
    @DeleteMapping("/{itemId}")
    CommonResponse<String> deleteCart(@PathVariable String itemId, HttpSession session){
        CommonResponse<Account> commonResponse=isLogin(session);
        if (commonResponse.getStatus() == 0) {
            return CommonResponse.createForError("未登录");
        }
        return cartService.DeleteCart(itemId,commonResponse.getData().getUserid());
    }

    //通过itemId更新购物车
    @LogAnnotation(module="carts",operation = "更新购物车")
    @PutMapping("")
    CommonResponse<Cart> updateCart(@RequestBody Cart cart,HttpSession session){
        CommonResponse<Account> commonResponse=isLogin(session);
        if (commonResponse.getStatus() == 0) {
            return CommonResponse.createForError("未登录");
        }
        cart.setUsername(commonResponse.getData().getUserid());
        return cartService.UpdateCart(cart);
    }
    @GetMapping("browse/{userid}")
    public CommonResponse<List<Cart>> browseCarts(@PathVariable String userid){
        List<Cart> originList =  cartService.getCartList(userid).getData();

        int length = originList.size();
        if(length == 0){
            return CommonResponse.createForError("您还没有订单信息!");
        }
        if(length <=3){
            return CommonResponse.createForSuccess("查找成功",originList);
        }else {
            return CommonResponse.createForSuccess("查找成功",originList.subList(0,3));
        }


    }
}
